package com.ruoyi.ysNewVideo.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.Threads;
import com.ruoyi.ysNewVideo.domain.NewDeviceInfo;
import com.ruoyi.ysNewVideo.domain.NewPresetInfo;
import com.ruoyi.ysNewVideo.domain.NewVideoDao;
import com.ruoyi.ysNewVideo.domain.NewVideoTokenDao;
import com.ruoyi.ysNewVideo.mapper.NewVideoMapper;
import com.ruoyi.ysNewVideo.service.NewVideoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: class by cxf
 * @author: AbrahamBill
 * @date: 2022/8/12
 **/
@Service
@Slf4j
public class NewVideoServiceImpl implements NewVideoService {

    @Autowired
    private NewVideoMapper newVideoMapper;

    /**
     * 萤石云 devices Url
     */
    @Value("${ys7.ys7DevicesList}")
    private String ys7DevicesList;

    /**
     * 新增 token 记录
     *
     * @param newVideoDao
     * @return
     */
    @Override
    public int insertVideoUrl(NewVideoDao newVideoDao) {
        newVideoDao.setUpdateTime(DateUtils.getNowDate());
        return newVideoMapper.insertVideoUrl(newVideoDao);
    }
    /**
     * 查询最后一个视频设备状态
     *
     * @return
     */
    @Override
    public NewVideoDao selectVideoLast() {
        return newVideoMapper.selectVideoLast();
    }
    /**
     * 更新最后一个视频设备状态
     *
     * @return
     */
    @Override
    public int updateVideoLast(NewVideoDao newVideoDao) {
        return newVideoMapper.updateVideoLast(newVideoDao);
    }
    /**
     * 更新视频 Token
     *
     * @param newVideoTokenDao
     * @return
     */
    @Override
    public int updateToken(NewVideoTokenDao newVideoTokenDao) {
        return newVideoMapper.updateToken(newVideoTokenDao);
    }
    /**
     * 更新过期时间
     *
     * @param newVideoTokenDao
     * @return
     */
    @Override
    public int updateTokenExpireTime(NewVideoTokenDao newVideoTokenDao) {
        return newVideoMapper.updateTokenExpireTime(newVideoTokenDao);
    }
    /**
     * 查找视频设备列表
     *
     * @param newVideoDao
     * @return
     */
    @Override
    public List<NewVideoDao> selectVideoDevice(NewVideoDao newVideoDao) {
        return newVideoMapper.selectVideoDevice(newVideoDao);
    }
    /**
     * 插入抓拍图片
     *
     * @param newVideoDao url数据信息
     * @return 影响行数
     */
    @Override
    public int insertCaptureImg(NewVideoDao newVideoDao) {
        return newVideoMapper.insertCaptureImg(newVideoDao);
    }
    /**
     * 查抓拍图片列表
     * @param deviceSerial 设备序列号
     * @return 查询列表
     */
    @Override
    public List<NewVideoDao> selectCaptureImgList(String deviceSerial) {
        return newVideoMapper.selectCaptureImgList(deviceSerial);
    }
    /**
     * 查询设备列表
     * @return 返回消息
     */
    @Override
    public List<NewDeviceInfo> ys7DeviceList() {
        NewVideoDao videoLast = selectVideoLast();
        /* 拼接获取token的URL */
        String getDevicesListUrl = ys7DevicesList + "?accessToken=" + videoLast.getAccessToken();
        List<NewDeviceInfo> NewDeviceInfos = null;
        try {
            ResponseEntity<String> responseEntity = Threads.postRequest(getDevicesListUrl);
            log.info(responseEntity.getBody());
            JSONObject jsonObject = JSON.parseObject(responseEntity.getBody());
            Object data = jsonObject.get("data");
            /* 拿到data数据 */
            NewDeviceInfos = JSON.parseArray(data.toString(), NewDeviceInfo.class);
            assert NewDeviceInfos != null;
            log.info(String.valueOf(NewDeviceInfos.size()));
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return NewDeviceInfos;
    }

    @Override
    public int insertPreset(NewPresetInfo newPresetInfo) {
        return newVideoMapper.insertPreset(newPresetInfo);
    }

    @Override
    public int deletePreset(NewPresetInfo newPresetInfo) {
        return newVideoMapper.deletePreset(newPresetInfo);
    }

    @Override
    public List<NewPresetInfo> selectPresetBySerial(NewPresetInfo newPresetInfo) {
        return newVideoMapper.selectPresetBySerial(newPresetInfo);
    }

    @Override
    public Integer deleteInfo(int id) {
        return newVideoMapper.deleteInfo(id);
    }
}
